/*
* 订单进度
* */
import 'package:center_beijing/api/statistical_analysis_api.dart';
import 'package:center_beijing/common/logutil.dart';
import 'package:center_beijing/common/styles.dart';
import 'package:center_beijing/config/global_config.dart';
import 'package:center_beijing/models/selectItem.dart';
import 'package:center_beijing/widgets/custom_loading_widget.dart';
import 'package:center_beijing/widgets/pull_select_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class OrderProgress extends StatefulWidget {
  @override
  _OrderProgressState createState() => _OrderProgressState();
}

class _OrderProgressState extends State<OrderProgress> with SingleTickerProviderStateMixin {
  CustomLoading loading = CustomLoading.withMessage('请稍候...');
  List<SelectItems> typesList = [SelectItems(label: '全部类型', value: '-1'),SelectItems(label: '房建工程', value: '1'),SelectItems(label: '市政工程', value: '2'),SelectItems(label: '路桥工程', value: '3')];
  SelectItems selectType = SelectItems(label: '全部类型', value: '-1');
  EasyRefreshController _controller = EasyRefreshController();
  List dataSource = [];
  int page = 1;
  int pageSize = 15;
  bool firstLoad = true;

  @override
  void initState() {
    super.initState();
    this.getInfo();
  }

  getInfo() async {
    var pass = {
      'projectId': GlobalConfig.selectFactory.value,
      'page': page,
      'page-size': pageSize,
      'type':selectType.value
    };
    var result = await getOrderProgress(pass);
    loading.dismiss();
    if (this.page == 1) {
      dataSource.clear();
    }
    firstLoad = false;
    if (result != null) {
      if (result.length == 0) {
      } else {
        dataSource.addAll(result);
      }
    }
    this.setState(() {});
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Styles.MainBackgroundColor,
      child: Column(
        children: [
          Container(
            color: Colors.white,
            child: PullSelectWidget(
              items: typesList,
              backColor: Colors.white,
              text_align: TextAlign.center,
              rightText: selectType.label,
              selectCallback: (value) {
                page=1;
                this.selectType = value;
                this.getInfo();
              },
            ),
          ),
          Expanded(child: this.dataSource.length > 0
              ? EasyRefresh.custom(
              shrinkWrap: true,
              firstRefresh: false,
              controller: _controller,
              header: MaterialHeader(),
              footer: ClassicalFooter(
                  loadingText: '加载中',
                  loadText: '上拉加载',
                  noMoreText: '没有更多数据',
                  loadedText: '加载完成',
                  infoText: '更新于 %T',
                  textColor: Styles.NoMoreColor,
                  showInfo: false),
              emptyWidget: (dataSource.length == 0 && !firstLoad)
                  ? Center(
                child: Image(
                  image: AssetImage('assets/images/no_data.png'),
                  width: 128,
                  height: 150,
                ),
              )
                  : null,
              onRefresh: () async {
                // _controller.finishLoad(success: true, noMore: false);
                if (mounted) {
                  page = 1;
                  this.getInfo();
                }
              },
              onLoad: () async {
                await Future.delayed(Duration(seconds: 2), () {
                  if (mounted) {
                    page++;
                    this.getInfo();
                  }
                });
              },
              slivers: <Widget>[
                SliverList(
                  delegate: SliverChildBuilderDelegate(
                        (context, index) {
                      return Container(
                        color: Colors.white,
                        margin: EdgeInsets.only(bottom: Styles.spacing8),
                        padding:
                        EdgeInsets.fromLTRB(0, 0, 0, Styles.spacing8),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Container(
                              alignment: Alignment.centerLeft,
                              decoration: BoxDecoration(
                                  border: Border(
                                      bottom: BorderSide(
                                          color: Styles.LineColor,
                                          width: 1))),
                              padding: EdgeInsets.fromLTRB(
                                  Styles.spacing8,
                                  Styles.spacing12,
                                  Styles.spacing8,
                                  Styles.spacing12),
                              child: Text(
                                '${this.dataSource[index]['orderName']}',
                                style: TextStyle(
                                    color: Styles.DetailColor,
                                    fontSize: Styles.Middle16Font,fontWeight: Styles.FontMainWeight),
                              ),
                            ),
                            Row(
                              children: [
                                Container(
                                  padding: EdgeInsets.fromLTRB(
                                      Styles.spacing8,
                                      Styles.spacing8,
                                      Styles.spacing8,
                                      Styles.spacing8),
                                  child: Text(
                                    '完成率',
                                    style: TextStyle(
                                        color: Styles.DetailColor,
                                        fontSize: Styles.MiddleFont17),
                                  ),
                                ),
                                Expanded(
                                    child: LinearProgressIndicator(
                                      backgroundColor: Styles.GrayLight,
                                      minHeight: 16.h,
                                      value: this.dataSource[index]['schedule'],
                                      color: Styles.OrangeColor,
                                    )),
                                Container(
                                  padding: EdgeInsets.fromLTRB(
                                      Styles.spacing8,
                                      Styles.spacing8,
                                      Styles.spacing8,
                                      Styles.spacing8),
                                  child: Text(
                                    '${ (this.dataSource[index]['schedule']*100).toStringAsFixed(0)}%',
                                    style: TextStyle(
                                        color: Styles.LeftTitleColor,
                                        fontSize: Styles.NormalFont),
                                  ),
                                ),
                              ],
                            ),
                            Container(
                              color: Styles.GrayLight,
                              padding: EdgeInsets.fromLTRB(
                                  Styles.spacing8,
                                  Styles.spacing8,
                                  Styles.spacing8,
                                  Styles.spacing8),
                              margin: EdgeInsets.fromLTRB(
                                  Styles.spacing8,
                                  Styles.spacing4,
                                  Styles.spacing8,
                                  Styles.spacing4),
                              child: Row(
                                mainAxisAlignment:
                                MainAxisAlignment.spaceBetween,
                                children: [
                                  Container(
                                    child: Text(
                                      '订单总量',
                                      style: TextStyle(
                                          color: Styles.DetailColor2,
                                          fontSize: Styles.NormalFont),
                                    ),
                                  ),
                                  Container(
                                    child: RichText(
                                        text: TextSpan(children: [
                                          TextSpan(
                                            text:
                                            '${this.dataSource[index]['totalVolume']!=null?this.dataSource[index]['totalVolume']:'0'}',
                                            style: TextStyle(
                                                fontSize: Styles.MiddleFont17,
                                                color: Styles.BlueColor),
                                          ),
                                          TextSpan(
                                            text: ' m³',
                                            style: TextStyle(
                                                fontSize: Styles.NormalFont,
                                                color: Styles.LeftTitleColor),
                                          )
                                        ])),
                                  ),
                                  Container(
                                    child: RichText(
                                        text: TextSpan(children: [
                                          TextSpan(
                                            text:
                                            '${this.dataSource[index]['totalQuantity']!=null?this.dataSource[index]['totalQuantity']:'0'}',
                                            style: TextStyle(
                                                fontSize: Styles.MiddleFont17,
                                                color: Styles.BlueColor),
                                          ),
                                          TextSpan(
                                            text: ' 片',
                                            style: TextStyle(
                                                fontSize: Styles.NormalFont,
                                                color: Styles.LeftTitleColor),
                                          )
                                        ])),
                                  ),
                                ],
                              ),
                            ),
                            Container(
                              color: Styles.GrayLight,
                              padding: EdgeInsets.fromLTRB(
                                  Styles.spacing8,
                                  Styles.spacing8,
                                  Styles.spacing8,
                                  Styles.spacing8),
                              margin: EdgeInsets.fromLTRB(
                                  Styles.spacing8,
                                  Styles.spacing4,
                                  Styles.spacing8,
                                  Styles.spacing4),
                              child: Row(
                                mainAxisAlignment:
                                MainAxisAlignment.spaceBetween,
                                children: [
                                  Container(
                                    child: Text(
                                      '完成总量',
                                      style: TextStyle(
                                          color: Styles.DetailColor2,
                                          fontSize: Styles.NormalFont),
                                    ),
                                  ),
                                  Container(
                                    child: RichText(
                                        text: TextSpan(children: [
                                          TextSpan(
                                            text:
                                            '${this.dataSource[index]['completeVolume']}',
                                            style: TextStyle(
                                                fontSize: Styles.MiddleFont17,
                                                color: Styles.BlueColor),
                                          ),
                                          TextSpan(
                                            text: ' m³',
                                            style: TextStyle(
                                                fontSize: Styles.NormalFont,
                                                color: Styles.LeftTitleColor),
                                          )
                                        ])),
                                  ),
                                  Container(
                                    child: RichText(
                                        text: TextSpan(children: [
                                          TextSpan(
                                            text:
                                            '${this.dataSource[index]['completeQuantity']}',
                                            style: TextStyle(
                                                fontSize: Styles.MiddleFont17,
                                                color: Styles.BlueColor),
                                          ),
                                          TextSpan(
                                            text: ' 片',
                                            style: TextStyle(
                                                fontSize: Styles.NormalFont,
                                                color: Styles.LeftTitleColor),
                                          )
                                        ])),
                                  ),
                                ],
                              ),
                            ),
                            Container(
                              color: Styles.GrayLight,
                              padding: EdgeInsets.fromLTRB(
                                  Styles.spacing8,
                                  Styles.spacing8,
                                  Styles.spacing8,
                                  Styles.spacing8),
                              margin: EdgeInsets.fromLTRB(
                                  Styles.spacing8,
                                  Styles.spacing4,
                                  Styles.spacing8,
                                  Styles.spacing4),
                              child: Row(
                                mainAxisAlignment:
                                MainAxisAlignment.spaceBetween,
                                children: [
                                  Container(
                                    child: Text(
                                      '待生产',
                                      style: TextStyle(
                                          color: Styles.DetailColor2,
                                          fontSize: Styles.NormalFont),
                                    ),
                                  ),
                                  Container(
                                    child: RichText(
                                        text: TextSpan(children: [
                                          TextSpan(
                                            text:
                                            '${this.dataSource[index]['waitVolume']}',
                                            style: TextStyle(
                                                fontSize: Styles.MiddleFont17,
                                                color: Styles.BlueColor),
                                          ),
                                          TextSpan(
                                            text: ' m³',
                                            style: TextStyle(
                                                fontSize: Styles.NormalFont,
                                                color: Styles.LeftTitleColor),
                                          )
                                        ])),
                                  ),
                                  Container(
                                    child: RichText(
                                        text: TextSpan(children: [
                                          TextSpan(
                                            text:
                                            '${this.dataSource[index]['waitQuantity']!=null?this.dataSource[index]['waitQuantity']:'0'}',
                                            style: TextStyle(
                                                fontSize: Styles.MiddleFont17,
                                                color: Styles.BlueColor),
                                          ),
                                          TextSpan(
                                            text: ' 片',
                                            style: TextStyle(
                                                fontSize: Styles.NormalFont,
                                                color: Styles.LeftTitleColor),
                                          )
                                        ])),
                                  ),
                                ],
                              ),
                            ),


                            (this.dataSource[index]['selected'] != null &&
                                this.dataSource[index]['selected'])
                                ? Column(
                              children: [
                                Container(
                                  color: Styles.GrayLight,
                                  padding: EdgeInsets.fromLTRB(
                                      Styles.spacing8,
                                      Styles.spacing8,
                                      Styles.spacing8,
                                      Styles.spacing8),
                                  margin: EdgeInsets.fromLTRB(
                                      Styles.spacing8,
                                      Styles.spacing4,
                                      Styles.spacing8,
                                      Styles.spacing4),
                                  child: Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                    children: [
                                      Container(
                                        child: Text(
                                          '生产中',
                                          style: TextStyle(
                                              color:
                                              Styles.DetailColor2,
                                              fontSize:
                                              Styles.NormalFont),
                                        ),
                                      ),
                                      Container(
                                        child: RichText(
                                            text: TextSpan(children: [
                                              TextSpan(
                                                text:
                                                '${this.dataSource[index]['productionVolume'] * 100}',
                                                style: TextStyle(
                                                    fontSize:
                                                    Styles.MiddleFont17,
                                                    color:
                                                    Styles.BlueColor),
                                              ),
                                              TextSpan(
                                                text: ' m³',
                                                style: TextStyle(
                                                    fontSize:
                                                    Styles.NormalFont,
                                                    color: Styles
                                                        .LeftTitleColor),
                                              )
                                            ])),
                                      ),
                                      Container(
                                        child: RichText(
                                            text: TextSpan(children: [
                                              TextSpan(
                                                text:
                                                '${this.dataSource[index]['productionQuantity']}',
                                                style: TextStyle(
                                                    fontSize:
                                                    Styles.MiddleFont17,
                                                    color:
                                                    Styles.BlueColor),
                                              ),
                                              TextSpan(
                                                text: ' 片',
                                                style: TextStyle(
                                                    fontSize:
                                                    Styles.NormalFont,
                                                    color: Styles
                                                        .LeftTitleColor),
                                              )
                                            ])),
                                      ),
                                    ],
                                  ),
                                ),
                                Container(
                                  color: Styles.GrayLight,
                                  padding: EdgeInsets.fromLTRB(
                                      Styles.spacing8,
                                      Styles.spacing8,
                                      Styles.spacing8,
                                      Styles.spacing8),
                                  margin: EdgeInsets.fromLTRB(
                                      Styles.spacing8,
                                      Styles.spacing4,
                                      Styles.spacing8,
                                      Styles.spacing4),
                                  child: Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                    children: [
                                      Container(
                                        child: Text(
                                          '堆场中',
                                          style: TextStyle(
                                              color:
                                              Styles.DetailColor2,
                                              fontSize:
                                              Styles.NormalFont),
                                        ),
                                      ),
                                      Container(
                                        child: RichText(
                                            text: TextSpan(children: [
                                              TextSpan(
                                                text:
                                                '${this.dataSource[index]['warehouseVolume'] * 100}',
                                                style: TextStyle(
                                                    fontSize:
                                                    Styles.MiddleFont17,
                                                    color:
                                                    Styles.BlueColor),
                                              ),
                                              TextSpan(
                                                text: ' m³',
                                                style: TextStyle(
                                                    fontSize:
                                                    Styles.NormalFont,
                                                    color: Styles
                                                        .LeftTitleColor),
                                              )
                                            ])),
                                      ),
                                      Container(
                                        child: RichText(
                                            text: TextSpan(children: [
                                              TextSpan(
                                                text:
                                                '${this.dataSource[index]['warehouseQuantity']}',
                                                style: TextStyle(
                                                    fontSize:
                                                    Styles.MiddleFont17,
                                                    color:
                                                    Styles.BlueColor),
                                              ),
                                              TextSpan(
                                                text: ' 片',
                                                style: TextStyle(
                                                    fontSize:
                                                    Styles.NormalFont,
                                                    color: Styles
                                                        .LeftTitleColor),
                                              )
                                            ])),
                                      ),
                                    ],
                                  ),
                                ),
                                Container(
                                  color: Styles.GrayLight,
                                  padding: EdgeInsets.fromLTRB(
                                      Styles.spacing8,
                                      Styles.spacing8,
                                      Styles.spacing8,
                                      Styles.spacing8),
                                  margin: EdgeInsets.fromLTRB(
                                      Styles.spacing8,
                                      Styles.spacing4,
                                      Styles.spacing8,
                                      Styles.spacing4),
                                  child: Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                    children: [
                                      Container(
                                        child: Text(
                                          '运输量',
                                          style: TextStyle(
                                              color:
                                              Styles.DetailColor2,
                                              fontSize:
                                              Styles.NormalFont),
                                        ),
                                      ),
                                      Container(
                                        child: RichText(
                                            text: TextSpan(children: [
                                              TextSpan(
                                                text:
                                                '${this.dataSource[index]['transportVolume']!=null?(this.dataSource[index]['transportVolume'] * 100):'0'}',
                                                style: TextStyle(
                                                    fontSize:
                                                    Styles.MiddleFont17,
                                                    color:
                                                    Styles.BlueColor),
                                              ),
                                              TextSpan(
                                                text: ' m³',
                                                style: TextStyle(
                                                    fontSize:
                                                    Styles.NormalFont,
                                                    color: Styles
                                                        .LeftTitleColor),
                                              )
                                            ])),
                                      ),
                                      Container(
                                        child: RichText(
                                            text: TextSpan(children: [
                                              TextSpan(
                                                text:
                                                '${this.dataSource[index]['transportQuantity']!=null?this.dataSource[index]['transportQuantity']:'0'}',
                                                style: TextStyle(
                                                    fontSize:
                                                    Styles.MiddleFont17,
                                                    color:
                                                    Styles.BlueColor),
                                              ),
                                              TextSpan(
                                                text: ' 片',
                                                style: TextStyle(
                                                    fontSize:
                                                    Styles.NormalFont,
                                                    color: Styles
                                                        .LeftTitleColor),
                                              )
                                            ])),
                                      ),
                                    ],
                                  ),
                                ),
                                Container(
                                  color: Styles.GrayLight,
                                  padding: EdgeInsets.fromLTRB(
                                      Styles.spacing8,
                                      Styles.spacing8,
                                      Styles.spacing8,
                                      Styles.spacing8),
                                  margin: EdgeInsets.fromLTRB(
                                      Styles.spacing8,
                                      Styles.spacing4,
                                      Styles.spacing8,
                                      Styles.spacing4),
                                  child: Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                    children: [
                                      Container(
                                        child: Text(
                                          '退货量',
                                          style: TextStyle(
                                              color: Styles.DetailColor2,
                                              fontSize: Styles.NormalFont),
                                        ),
                                      ),
                                      Container(
                                        child: RichText(
                                            text: TextSpan(children: [
                                              TextSpan(
                                                text:
                                                '${this.dataSource[index]['returnVolume']!=null?this.dataSource[index]['returnVolume']:'0'}',
                                                style: TextStyle(
                                                    fontSize: Styles.MiddleFont17,
                                                    color: Styles.BlueColor),
                                              ),
                                              TextSpan(
                                                text: ' m³',
                                                style: TextStyle(
                                                    fontSize: Styles.NormalFont,
                                                    color: Styles.LeftTitleColor),
                                              )
                                            ])),
                                      ),
                                      Container(
                                        child: RichText(
                                            text: TextSpan(children: [
                                              TextSpan(
                                                text:
                                                '${this.dataSource[index]['returnQuantity']!=null?this.dataSource[index]['returnQuantity']:'0'}',
                                                style: TextStyle(
                                                    fontSize: Styles.MiddleFont17,
                                                    color: Styles.BlueColor),
                                              ),
                                              TextSpan(
                                                text: ' 片',
                                                style: TextStyle(
                                                    fontSize: Styles.NormalFont,
                                                    color: Styles.LeftTitleColor),
                                              )
                                            ])),
                                      ),
                                    ],
                                  ),
                                ),
                                Container(
                                  color: Styles.GrayLight,
                                  padding: EdgeInsets.fromLTRB(
                                      Styles.spacing8,
                                      Styles.spacing8,
                                      Styles.spacing8,
                                      Styles.spacing8),
                                  margin: EdgeInsets.fromLTRB(
                                      Styles.spacing8,
                                      Styles.spacing4,
                                      Styles.spacing8,
                                      Styles.spacing4),
                                  child: Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                    children: [
                                      Container(
                                        child: Text(
                                          '维修中',
                                          style: TextStyle(
                                              color: Styles.DetailColor2,
                                              fontSize: Styles.NormalFont),
                                        ),
                                      ),
                                      Container(
                                        child: RichText(
                                            text: TextSpan(children: [
                                              TextSpan(
                                                text:
                                                '${this.dataSource[index]['maintainVolume']!=null?this.dataSource[index]['maintainVolume']:'0'}',
                                                style: TextStyle(
                                                    fontSize: Styles.MiddleFont17,
                                                    color: Styles.BlueColor),
                                              ),
                                              TextSpan(
                                                text: ' m³',
                                                style: TextStyle(
                                                    fontSize: Styles.NormalFont,
                                                    color: Styles.LeftTitleColor),
                                              )
                                            ])),
                                      ),
                                      Container(
                                        child: RichText(
                                            text: TextSpan(children: [
                                              TextSpan(
                                                text:
                                                '${this.dataSource[index]['maintainQuantity']!=null?this.dataSource[index]['maintainQuantity']:'0'}',
                                                style: TextStyle(
                                                    fontSize: Styles.MiddleFont17,
                                                    color: Styles.BlueColor),
                                              ),
                                              TextSpan(
                                                text: ' 片',
                                                style: TextStyle(
                                                    fontSize: Styles.NormalFont,
                                                    color: Styles.LeftTitleColor),
                                              )
                                            ])),
                                      ),
                                    ],
                                  ),
                                ),
                                Container(
                                  color: Styles.GrayLight,
                                  padding: EdgeInsets.fromLTRB(
                                      Styles.spacing8,
                                      Styles.spacing8,
                                      Styles.spacing8,
                                      Styles.spacing8),
                                  margin: EdgeInsets.fromLTRB(
                                      Styles.spacing8,
                                      Styles.spacing4,
                                      Styles.spacing8,
                                      Styles.spacing4),
                                  child: Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                    children: [
                                      Container(
                                        child: Text(
                                          '已报废',
                                          style: TextStyle(
                                              color: Styles.DetailColor2,
                                              fontSize: Styles.NormalFont),
                                        ),
                                      ),
                                      Container(
                                        child: RichText(
                                            text: TextSpan(children: [
                                              TextSpan(
                                                text:
                                                '${this.dataSource[index]['scrapVolume']!=null?this.dataSource[index]['scrapVolume']:'0'}',
                                                style: TextStyle(
                                                    fontSize: Styles.MiddleFont17,
                                                    color: Styles.BlueColor),
                                              ),
                                              TextSpan(
                                                text: ' m³',
                                                style: TextStyle(
                                                    fontSize: Styles.NormalFont,
                                                    color: Styles.LeftTitleColor),
                                              )
                                            ])),
                                      ),
                                      Container(
                                        child: RichText(
                                            text: TextSpan(children: [
                                              TextSpan(
                                                text:
                                                '${this.dataSource[index]['scrapQuantity']!=null?this.dataSource[index]['scrapQuantity']:'0'}',
                                                style: TextStyle(
                                                    fontSize: Styles.MiddleFont17,
                                                    color: Styles.BlueColor),
                                              ),
                                              TextSpan(
                                                text: ' 片',
                                                style: TextStyle(
                                                    fontSize: Styles.NormalFont,
                                                    color: Styles.LeftTitleColor),
                                              )
                                            ])),
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            )
                                : Container(),
                            GestureDetector(
                              onTap: () {
                                this.setState(() {
                                  if (this.dataSource[index]['selected'] ==
                                      null) {
                                    this.dataSource[index]['selected'] = true;
                                  } else {
                                    this.dataSource[index]['selected'] =
                                    !this.dataSource[index]['selected'];
                                  }
                                });
                              },
                              behavior: HitTestBehavior.translucent,
                              child: Container(
                                alignment: Alignment.center,
                                padding: EdgeInsets.fromLTRB(
                                    0, Styles.spacing8, 0, Styles.spacing8),
                                child: Image.asset(
                                  (this.dataSource[index]['selected'] !=
                                      null &&
                                      this.dataSource[index]['selected'])
                                      ? 'assets/images/more2.png'
                                      : 'assets/images/more.png',
                                  width: 16.w,
                                  height: 16.h,
                                ),
                              ),
                            )
                          ],
                        ),
                      );
                    },
                    childCount: dataSource.length,
                  ),
                ),
              ])
              : firstLoad
              ? Center(
            child: CircularProgressIndicator(),
          )
              : Center(
            child: Image(
              image: AssetImage('assets/images/no_data.png'),
              width: 128,
              height: 150,
            ),
          ))
        ],
      ) ,
    );
  }
}
